﻿using  System;
using  System.Text;
using  System.Xml;
using  System.Xml.Serialization;
using  System.IO;

namespace com.ubosi.common
{
        /**////  <summary>
        ///  序列化与反序列化
        ///  FileName                        :        Serializer.cs      
        ///  Verion                          :        0.10
        ///  Author                          :              
        ///  Update                          :              
        ///  Description                     :        序列化与反序列化,主要用于将对象里的数据序列化成XML数据,用于存于文本或是数据库        
        ///  </summary>
        public  class  Serializer
        {
                /**////  <summary>
                ///  将文件反序列化成对象
                ///  使用:BlogSettingInfo  info  =    (BlogSettingInfo)  Serializer.XmlDeserializerFormFile(typeof(BlogSettingInfo),  @"H:CSBlog.xml");
                ///  </summary>
                ///  <param  name="type">对象类型</param>
                ///  <param  name="path">文件路径</param>
                ///  <returns></returns>
                public  static  object  XmlDeserializerFormFile(Type  type,  string  path)
                {
                        return  new  XmlSerializer(type).Deserialize(new  XmlTextReader(path));
                }


                /**////  <summary>
                ///  将字符串内容反序列化成对象
                ///  使用:BlogSettingInfo  info  =  (BlogSettingInfo)Serializer.XmlDeserializerFormText(typeof(BlogSettingInfo),config);
                ///  </summary>
                ///  <param  name="type">对象类型</param>
                ///  <param  name="serializeText">被序列化的文本</param>
                ///  <returns></returns>
                public  static  object  XmlDeserializerFormText(Type  type,  string  serializeText)
                {
                        using  (StringReader  reader  =  new  StringReader(serializeText))
                        {
                                return  new  XmlSerializer(type).Deserialize(reader);
                        }
                }


                /**////  <summary>
                ///  将目标对象序列化成XML到文件中
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <param  name="path"></param>
                public  static  void  XmlSerializerToFile(object  target,  string  path)
                {
                        //XmlTextWriter  writer  =  new  XmlTextWriter(path,  Encoding.UTF8);
                        StreamWriter  writer  =  new  StreamWriter(path);
                        new  XmlSerializer(target.GetType()).Serialize((StreamWriter)writer,  target);
                        writer.Close();
                }


                /**////  <summary>
                ///  将目标对象序列化成完整的XML文档
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <returns></returns>
                public  static  string  XmlSerializerToXml(object  target)
                {
                        return  XmlSerializerToText(target,  false);
                }


                /**////  <summary>
                ///  将目标对象序列化成XML文档内容(去除声明属性)
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <returns></returns>
                public  static  string  XmlSerializerToText(object  target)
                {
                        return  XmlSerializerToText(target,  true);
                }


                /**////  <summary>
                ///  将目标对象序列化成XML文档
                ///  </summary>
                ///  <param  name="target"></param>
                ///  <param  name="isText">是否去除声明属性</param>
                ///  <returns></returns>
                private  static  string  XmlSerializerToText(object  target,  bool  isText)
                {
                        StringWriter  writer  =  new  StringWriter();
                         
                        new  XmlSerializer(target.GetType()).Serialize((TextWriter)writer,  target);
                        StringBuilder  sb  =  writer.GetStringBuilder();
                        writer.Close();
                        if  (isText)
                        {
                                sb.Replace("<?xml  version=\"1.0\"  encoding=\"utf-16\"?> ",  "");
                                sb.Replace("  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",  "");
                        }
                        else
                        {
                                sb.Replace("utf-16",  "utf-8");
                        }
                        return  sb.ToString();
                }
               

        }

  

}